package com.lsh.oms.core.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.lsh.oms.core.constant.GoodsConstants;
import com.lsh.oms.core.model.order.AtpHoldDetail;
import com.lsh.oms.core.utils.model.Good;
import org.apache.commons.lang.StringUtils;
import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.message.BasicNameValuePair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;

/**
 * @author peter
 */
@Deprecated
public class GoodsServiceKaUtils extends BaseGoodUtils {

    private static Logger log = LoggerFactory.getLogger(GoodsServiceKaUtils.class);

    public static Map<String, Good> getKACode2GoodsMaps2(AtpHoldDetail atpHoldDetail, Map<Long, Long> orderDetailMap, String shopServiceUrl, Long venderId, String logSign) {
        JSONArray skuIds = new JSONArray();
        for (Long skuCode : atpHoldDetail.getSkuCodes()) {
            Long goodsCode = orderDetailMap.get(skuCode);
            if (null == goodsCode) {
                continue;
            }
            skuIds.add(Collections.singletonMap("sku_id", goodsCode));
        }

        if (skuIds.isEmpty()) {
            return Collections.EMPTY_MAP;
        }
        List<BasicNameValuePair> pairs = new ArrayList<>(5);
        pairs.add(new BasicNameValuePair("sku_ids", skuIds.toString()));
        pairs.add(new BasicNameValuePair(GoodsConstants.SUPPLY_ORG_NAME, String.valueOf(atpHoldDetail.getSupplierOrg())));
        pairs.add(new BasicNameValuePair(GoodsConstants.SUPPLY_DC_NAME, atpHoldDetail.getDc()));

        HttpEntity entity = new UrlEncodedFormEntity(pairs, Consts.UTF_8);
        shopServiceUrl = shopServiceUrl + SAAS_GETINFOS;
        log.info(logSign + "【KA 请求商品服务url:{}】【param is {}】", shopServiceUrl, JSON.toJSONString(pairs));
        Map<String, Object> goodsInfoResp = doReq(shopServiceUrl, entity, BaseGoodUtils.buildHeaders(venderId), logSign);

        if (!goodsInfoResp.get("ret").equals(0) || StringUtils.isEmpty(goodsInfoResp.get("content") + "")) {
            log.info(logSign + "【KA 商品服务返回结果失败】");
            return Collections.emptyMap();
        }
        JSONObject content = JSON.parseObject(goodsInfoResp.get("res").toString()).getJSONObject("content");
        Map<String, Good> code2GoodsMap = new HashMap<>();
        for (Long skuCode : atpHoldDetail.getSkuCodes()) {
            Long goodsCode = orderDetailMap.get(skuCode);
            JSONObject item = content.getJSONObject(goodsCode.toString());
            if (item == null) {
                log.error(logSign + "【KA 商品信息不存在！goodsCode:{}】", goodsCode);
                continue;
            }
            Good goods = new Good();
            goods.setGoodsCode(goodsCode);
            goods.setGoodsSaleUnit(item.getBigDecimal("sale_unit"));
            goods.setSkuCode(item.getLong("item_id"));
            goods.setSupplySkuCode(item.getString("code"));
            goods.setSupplierOrg(atpHoldDetail.getSupplierOrg());
            code2GoodsMap.put(item.getString("code"), goods);
        }

        return code2GoodsMap;
    }


}
